package com.example.opengl.render.video

import android.view.Surface
import com.alibaba.android.arouter.facade.annotation.Route
import com.android.baselibrary.base.BaseActivity
import com.android.baselibrary.video.VideoExt
import com.example.opengl.R
import com.example.opengl.databinding.ActivityOpenglRenderVideoBinding
import com.example.opengl.decoder.MediaCodecHelper
import com.example.opengl.main.OpenGLPath
import com.example.opengl.main.OpenGlMainActivity

@Route(path = OpenGLPath.OPENGL_RENDER_VIDEO)
class OpenGlRenderVideoActivity:BaseActivity<ActivityOpenglRenderVideoBinding>() {

    val  videoDrawer = VideoDrawer()
    override fun getLayoutId(): Int  = R.layout.activity_opengl_render_video

    override fun initView() {
       val videoInfo =  VideoExt.getVideoInfo(OpenGlMainActivity.VIDEO_PATH)

        videoDrawer.getSurfaceTexture {
            MediaCodecHelper.initMediaCodec(videoInfo.path, Surface(it))
        }
        videoDrawer.setVideoSize(videoInfo.width,videoInfo.height)
        mBinding.surface.setEGLContextClientVersion(2)
        mBinding.surface.setRenderer(VideoRender(videoDrawer))
    }

    override fun initListener() {
        mBinding.button.setOnClickListener {
            if (mBinding.button.text.equals("播放")) {
                MediaCodecHelper.stop()
                mBinding.button.text = "暂停"
            } else {
                mBinding.button.text = "播放"
                MediaCodecHelper.start()
            }
        }

    }

    override fun onDestroy() {
        super.onDestroy()
        MediaCodecHelper.release()
        videoDrawer.release()
    }


}